/**
 * 
 */
package classificationEtSelectionTest;

import general.Page;
import general.Point;

import java.util.Vector;

import classificationEtSelection.ClassificationEtSelection;
import classificationEtSelection.Cluster;

import junit.framework.TestCase;

/**
 * @author Lafarge
 *
 */
public class ClassificationEtSelectionTest extends TestCase {

	/**
	 * Test method for {@link classificationEtSelection.ClassificationEtSelection#lancerLaClassification(java.util.Vector)}.
	 */
	public void testLancerLaClassification() {
		ClassificationEtSelection test = new ClassificationEtSelection();
		Vector <Page> pages = new Vector <Page>(); 
		
		
		int nbPages = 10000;
		Vector <Float> vect;
		for(int i = 0; i < nbPages ;i++)
		{
			pages.add(new Page());
			
			vect = new Vector <Float>();
			vect.add(((float) Math.random()) * 2 - 1);
			pages.get(i).getPoint().setCritereDepth(vect);
			
			vect = new Vector <Float>();
			vect.add(((float) Math.random()) * 2 - 1);
			/*vect.add(((float) Math.random()) * 2 - 1);
			vect.add(((float) Math.random()) * 2 - 1);
			vect.add(((float) Math.random()) * 2 - 1);
			vect.add(((float) Math.random()) * 2 - 1);
			vect.add(((float) Math.random()) * 2 - 1);
			*/vect.add(((float) Math.random()) * 2 - 1);
			pages.get(i).getPoint().setCriteresDCT(vect);
			
			vect = new Vector <Float>();
			vect.add(((float) Math.random()) * 2 - 1);
			//vect.add(((float) Math.random()) * 2 - 1);
			//vect.add(((float) Math.random()) * 2 - 1);
			pages.get(i).getPoint().setCriteresTagP(vect);
			
			vect = new Vector <Float>();
			vect.add(((float) Math.random()) * 2 - 1);
			/*vect.add(((float) Math.random()) * 2 - 1);
			vect.add(((float) Math.random()) * 2 - 1);
			vect.add(((float) Math.random()) * 2 - 1);
			vect.add(((float) Math.random()) * 2 - 1);
			vect.add(((float) Math.random()) * 2 - 1);
			*/vect.add(((float) Math.random()) * 2 - 1);
			pages.get(i).getPoint().setCriteresURL(vect);

		}
				
		test.lancerLaClassification(pages);
		
		System.err.println("nombre de clusters");
		System.err.println(test.getClusters().size());
		System.err.println("centre des clusters");
		for(int i = 0; i < test.getClusters().size() ;i++)
		{
			System.err.println(test.getClusters().get(i).getCentre().getPoint());
		}
		
		int compteur = 0;
		float varianceIntraClasse = (float) 0;
		for(int i = 0; i < test.getClusters().size() ;i++)
		{
			varianceIntraClasse = varianceIntraClasse + test.getClusters().get(i).getVariance() * test.getClusters().get(i).getPages().size();
			if (test.getClusters().get(i).isUtile())
			{
				compteur++;
				System.err.println("Le cluster numero: " + i +" est utile" );	
			}
			else
			{
				System.err.println("Le cluster numero: " + i +" n'est pas utile" );
			}
			System.err.println("Sa variance est de " + test.getClusters().get(i).getVariance());
			System.err.println("Il comporte " + test.getClusters().get(i).getPages().size()+ " elements");
		}
		varianceIntraClasse = varianceIntraClasse / nbPages;
		System.err.println("Il y a " + compteur + " clusters utiles");
		System.err.println("Il y a " + (test.getClusters().size() - compteur) + " clusters inutiles");
		System.err.println("La variance intra-classe moyenne est de: " + varianceIntraClasse);
		
		
		
	}

	/**
	 * Test method for {@link classificationEtSelection.ClassificationEtSelection#majCentres()}.
	 */
	public void testMajCentres() {
		Vector <Cluster> clusters = new Vector <Cluster>();
		ClassificationEtSelection test = new ClassificationEtSelection();
		
		
		Cluster cluster = new Cluster();
		
		Point point1 = new Point();
		Point point2 = new Point();
		
		Vector <Float> dct1 = new Vector <Float>();
		dct1.add((float) -1.0);
		dct1.add((float) 2.0);
		point1.setCriteresDCT(dct1);

		Vector <Float>  depth1 = new Vector <Float>();
		depth1.add((float) 3.0);
		point1.setCritereDepth(depth1);
		
		Vector <Float>  criteresTagP1 = new Vector <Float>();
		criteresTagP1.add((float) 3.0);
		criteresTagP1.add((float) 6.0);
		criteresTagP1.add((float) 8.0);
		point1.setCriteresTagP(criteresTagP1);
		
		
		Vector <Float>  criteresURL1 = new Vector <Float>();
		criteresURL1.add((float) 2.0);
		criteresURL1.add((float) 3.0);
		criteresURL1.add((float) 4.0);
		point1.setCriteresURL(criteresURL1);
		
		
		// CLUSTER 2
		Vector <Float> dct2 = new Vector <Float>();
		dct2.add((float)  1.0);
		dct2.add((float)  -2.0);
		point2.setCriteresDCT(dct2);

		Vector <Float>  depth2 = new Vector <Float>();
		depth2.add((float)  5.0);
		point2.setCritereDepth(depth2);
		
		Vector <Float>  criteresTagP2 = new Vector <Float>();
		criteresTagP2.add((float)  -3.0);
		criteresTagP2.add((float)  -6.0);
		criteresTagP2.add((float)  -8.0);
		point2.setCriteresTagP(criteresTagP2);
		
		Vector <Float>  criteresURL2 = new Vector <Float>();
		criteresURL2.add((float)  -2.0);
		criteresURL2.add((float)  -3.0);
		criteresURL2.add((float)  -4.0);
		point2.setCriteresURL(criteresURL2);
		
		
		Page page1 = new Page();
		Page page2 = new Page();
		
		page1.setPoint(point1);
		page2.setPoint(point2);
		
		cluster.ajouterPage(page1);
		cluster.ajouterPage(page2);
		clusters.add(cluster);
		test.setClusters(clusters);
		

		test.majCentres();
		
		System.err.println("Resultat attendu");
		System.err.println("[[4],[0,0],[0,0,0],[0,0,0]");
		System.err.println("Resultat obtenu");
		System.err.println(test.getClusters().get(0).getCentre().getPoint());
		
	}

	/**
	 * Test method for {@link classificationEtSelection.ClassificationEtSelection#videsClusters()}.
	 */
	public void testVidesClusters() {
		Vector <Cluster> clusters = new Vector <Cluster>();
		ClassificationEtSelection test = new ClassificationEtSelection();
		
		
		Cluster cluster1 = new Cluster();
		Cluster cluster2 = new Cluster();
		Cluster cluster3 = new Cluster();
		Cluster cluster4 = new Cluster();
		
		clusters.add(cluster1);	
		clusters.add(cluster2);
		clusters.add(cluster3);
		clusters.add(cluster4);
		
		test.setClusters(clusters);
		
		test.videsClusters();
		
		assertEquals(0, test.getClusters().get(0).getPages().size() + test.getClusters().get(1).getPages().size() + test.getClusters().get(2).getPages().size() + test.getClusters().get(3).getPages().size());
		
	}

	/**
	 * Test method for {@link classificationEtSelection.ClassificationEtSelection#attribuerPageCluster(general.Page)}.
	 */
	public void testAttribuerPageCluster() {
		Vector <Cluster> clusters = new Vector <Cluster>();
		ClassificationEtSelection test = new ClassificationEtSelection();
		
		
		Cluster cluster1 = new Cluster();
		Cluster cluster2 = new Cluster();

		
		//Centre des clusters
		Point centre1 = new Point();
		Point centre2 = new Point();
		Point point = new Point();
		
		
		// CLUSTER 1
		Vector <Float> dct1 = new Vector <Float>();
		dct1.add((float) 1.0);
		dct1.add((float) 2.0);
		centre1.setCriteresDCT(dct1);

		Vector <Float>  depth1 = new Vector <Float>();
		depth1.add((float) 3.0);
		centre1.setCritereDepth(depth1);
		
		Vector <Float>  criteresTagP1 = new Vector <Float>();
		criteresTagP1.add((float) 3.0);
		criteresTagP1.add((float) 6.0);
		criteresTagP1.add((float) 8.0);
		centre1.setCriteresTagP(criteresTagP1);
		
		
		Vector <Float>  criteresURL1 = new Vector <Float>();
		criteresURL1.add((float) 2.0);
		criteresURL1.add((float) 3.0);
		criteresURL1.add((float) 4.0);
		centre1.setCriteresURL(criteresURL1);
		
		
		// CLUSTER 2
		Vector <Float> dct2 = new Vector <Float>();
		dct2.add((float) 2.0);
		dct2.add((float) 2.0);
		centre2.setCriteresDCT(dct2);

		Vector <Float>  depth2 = new Vector <Float>();
		depth2.add((float) 3.0);
		centre2.setCritereDepth(depth2);
		
		Vector <Float>  criteresTagP2 = new Vector <Float>();
		criteresTagP2.add((float) 3.0);
		criteresTagP2.add((float) 6.0);
		criteresTagP2.add((float) 8.0);
		centre2.setCriteresTagP(criteresTagP2);
		
		Vector <Float>  criteresURL2 = new Vector <Float>();
		criteresURL2.add((float) 2.0);
		criteresURL2.add((float) 3.0);
		criteresURL2.add((float) 5.0);
		centre2.setCriteresURL(criteresURL2);
		
		
		// PAGE
		Vector <Float> dct = new Vector <Float>();
		dct.add((float) 1.0);
		dct.add((float) 2.0);
		point.setCriteresDCT(dct);

		Vector <Float>  depth = new Vector <Float>();
		depth.add((float) 3.0);
		point.setCritereDepth(depth);
		
		Vector <Float>  criteresTagP = new Vector <Float>();
		criteresTagP.add((float) 3.0);
		criteresTagP.add((float) 6.0);
		criteresTagP.add((float) 8.0);
		point.setCriteresTagP(criteresTagP);
		
		Vector <Float>  criteresURL = new Vector <Float>();
		criteresURL.add((float) 2.0);
		criteresURL.add((float) 3.0);
		criteresURL.add((float) 4.0);
		point.setCriteresURL(criteresURL);
		
		cluster1.setCentre(centre1);
		cluster2.setCentre(centre2);
		
		
		clusters.add(cluster1);	
		clusters.add(cluster2);
		test.setClusters(clusters);
		
		Page page = new Page();
		page.setPoint(point);
			
		assertEquals(1,test.attribuerPageCluster(page));
		
	}

	/**
	 * Test method for {@link classificationEtSelection.ClassificationEtSelection#ClassificationEtSelection()}.
	 */
	public void testClassificationEtSelection() {
		fail("Not yet implemented");
	}

	/**
	 * Test method for {@link classificationEtSelection.ClassificationEtSelection#ClassificationEtSelection(boolean)}.
	 */
	public void testClassificationEtSelectionBoolean() {
		fail("Not yet implemented");
	}

}
